package cn.attackme.myuploader.controller;

import cn.attackme.myuploader.model.Indicator;
import cn.attackme.myuploader.model.Section;
import cn.attackme.myuploader.service.IndicatorService;
import cn.attackme.myuploader.service.SectionService;
import cn.attackme.myuploader.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/section")
@CrossOrigin
public class SectionController {

    @Autowired
    private SectionService sectionService;
    @PostMapping("/find")
    public Map<String,Object> find(Long id) throws IOException {
        System.out.println(id);
        Section section=sectionService.getSection(id);
        Map<String,Object> resultMap = ResultUtils.getResponse(section,"succeed",
                "internal error",false);
        return resultMap;
    }


    @PostMapping("/add")
    public Map<String,Object> add(Section section) throws IOException {
        System.out.println(section);
        int  flag =sectionService.add(section);
        System.out.println(section.toString());
        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/update")
    public Map<String,Object> update(Section section) throws IOException {
        System.out.println(section);
        int  flag =sectionService.update(section);
        System.out.println(section.toString());
        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/delete")
    public Map<String,Object> delete(Long id) throws IOException {
        int  flag =sectionService.delete(id);
        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/all")
    public Map<String,Object>  all() throws IOException {
        List<Map<String,Object>> allList =  sectionService.fandAllList();
        Map<String,Object> resultMap = ResultUtils.getResponse(allList,"succeed",
                "internal error",false);
        return resultMap;
    }
}
